package cn.edu.zust.se.mvcdb.servlet;

import cn.edu.zust.se.mvcdb.service.UserService;
import cn.edu.zust.se.mvcdb.service.impl.UserServiceImpl;
import cn.edu.zust.se.mvcdb.servlet.base.BaseServlet;
import cn.edu.zust.se.mvcdb.vo.User;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "loginServlet",urlPatterns = "/login")
public class UserLoginServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        UserService userService=(UserService) getServletContext().getAttribute("userService");
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        User u=userService.login(userName,password);
        if(u!=null){
            req.getSession().setAttribute("user",u);
            req.getRequestDispatcher("home.jsp").forward(req,resp);
        }else
            req.getRequestDispatcher("login.jsp").forward(req,resp);
    }
}
